package com.pengxh.app.gb28181.util;

import android.app.Activity;
import android.hardware.camera2.CameraCharacteristics;
import android.util.Log;
import android.util.Size;
import android.view.Surface;

import java.util.ArrayList;
import java.util.List;

/**
 * 常规手机相机{@link android.hardware.camera2.CameraDevice}模块参数获取和计算
 */
public class CameraDeviceHelper {

    private static final String TAG = "CameraDeviceHelper";

    /**
     * Android 相机硬件的“自然方向”是横屏。即使你在竖屏下拍照或预览，相机输出的图像依然是横屏方向。
     * 前置摄像头需要转换270度
     * 后置摄像头需要转换90度
     * <a href="https://github.com/byhook/ffmpeg4android/blob/master/ffmpeg-camera-stream/src/main/java/com/onzhou/ffmpeg/camera/CameraV1.java">...</a>
     */
    public static int getFrameRotateDegree(Activity activity, CameraCharacteristics characteristics) {
        int rotation = activity.getWindowManager().getDefaultDisplay().getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        // 获取摄像头方向
        int sensorOrientation = characteristics.get(CameraCharacteristics.SENSOR_ORIENTATION);
        int facing = characteristics.get(CameraCharacteristics.LENS_FACING);

        /**
         * 计算画面最终需要旋转的角度
         * <p>
         * 设备处于竖屏状态（0度）
         * 前置摄像头传感器方向是270度
         * 经过镜像补偿计算后，最终方向是90度
         * 对于前置摄像头，90度的旋转角度意味着视频需要逆时针旋转90度才能正常显示。
         * */
        int rotateDegree;
        if (facing == CameraCharacteristics.LENS_FACING_FRONT) {
            // 前置摄像头
            rotateDegree = (sensorOrientation + degrees) % 360;
            rotateDegree = (360 - rotateDegree) % 360;  // 补偿镜像
        } else {
            // 后置摄像头
            rotateDegree = (sensorOrientation - degrees + 360) % 360;
        }

        Log.d(TAG, "display rotation: " + degrees + ", " + "sensorOrientation:" + sensorOrientation + ", " + "rotateDegree: " + rotateDegree);
        return rotateDegree;
    }

    /**
     * Android 相机硬件的“自然方向”是横屏。即使你在竖屏下拍照或预览，相机输出的图像依然是横屏方向。
     */
    public static Size chooseOptimalSize(Size[] choices, int screenWidth, int screenHeight, double targetRatio) {
        List<Size> sizes = new ArrayList<>();
        for (Size size : choices) {
            double ratio = (double) size.getWidth() / size.getHeight();
            if (ratio == targetRatio) {
                sizes.add(size);
            }
        }

        Log.d(TAG, "屏幕尺寸: [" + screenWidth + ", " + screenHeight + "]");

        Size optimalSize = null;
        long maxPixels = 0;

        for (Size size : sizes) {
            Log.d(TAG, "可用尺寸: [" + size.getWidth() + ", " + size.getHeight() + "]");
            // 选择不超过屏幕尺寸且像素数最大的
            if (size.getWidth() <= Math.max(screenWidth, screenHeight) &&
                    size.getHeight() <= Math.min(screenWidth, screenHeight)) {
                long pixels = (long) size.getWidth() * size.getHeight();
                if (pixels > maxPixels) {
                    maxPixels = pixels;
                    optimalSize = size;
                }
            }
        }

        // 如果没有找到合适的，选择最小的
        if (optimalSize == null && !sizes.isEmpty()) {
            optimalSize = sizes.get(0);
            for (Size size : sizes) {
                if (size.getWidth() < optimalSize.getWidth() &&
                        size.getHeight() < optimalSize.getHeight()) {
                    optimalSize = size;
                }
            }
        }

        Log.d(TAG, "最佳尺寸: [" + optimalSize.getWidth() + ", " + optimalSize.getHeight() + "]");
        return optimalSize;
    }
}
